package work.huangxin.share.controller.admin;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import work.huangxin.share.domain.other.ArticleMessage;
import work.huangxin.share.domain.other.CommentMessage;
import work.huangxin.share.domain.other.NoticeMessage;
import work.huangxin.share.mapper.ArticleMessageMapper;
import work.huangxin.share.mapper.CommentMessageMapper;
import work.huangxin.share.service.AdminArticleService;
import work.huangxin.share.service.impl.ArticleOperationService;
import work.huangxin.share.service.impl.CommentService;
import work.huangxin.share.service.impl.NoticeOperationService;
import work.huangxin.share.util.status.BaseResponse;
import work.huangxin.share.util.status.CodeEnum;
import work.huangxin.share.util.status.ResponseData;

import javax.annotation.Resource;
import java.util.Objects;

@RestController
@RequestMapping("/wx/admin/api/adminArticle")
public class AdminArticleController {

    @Autowired
    private AdminArticleService adminArticleService;

    @Autowired
    private ArticleOperationService articleOperationService;

    @Autowired
    private CommentService commentService;

    @Resource
    private ArticleMessageMapper articleMessageMapper;

    @Resource
    private CommentMessageMapper commentMessageMapper;

    @Autowired
    private NoticeOperationService noticeOperationService;


    //后台分页查询所有的文章
    @PostMapping("/getAllArticle")
    public BaseResponse getAllReportArticle(@RequestBody ArticleMessage articleMessage ,
                                            @RequestParam Integer pageNum,
                                            @RequestParam Integer pageSize) {
        PageInfo<ArticleMessage> PageInfo = adminArticleService.selectPageArticleList(articleMessage,pageNum, pageSize);
        if (Objects.isNull(PageInfo)) {
            return ResponseData.out(CodeEnum.NOT_FOUND, "数据丢失");
        }
        return ResponseData.out(CodeEnum.SUCCESS, PageInfo);
    }

    //后台分页查询所有评论
    @PostMapping("/getAllComments")
    public BaseResponse getAllComments( @RequestBody CommentMessage commentMessage,
                                        @RequestParam Integer pageNum,
                                        @RequestParam Integer pageSize) {
        PageInfo<CommentMessage> pageInfo =adminArticleService.selectPageCommentList(commentMessage, pageNum, pageSize);
        if (Objects.isNull(pageInfo)) {
            return ResponseData.out(CodeEnum.NOT_FOUND, "数据丢失");
        }
        return ResponseData.out(CodeEnum.SUCCESS, pageInfo);
    }

    //后台删除用户发布的文章
    @DeleteMapping("/deleteArticle/{articleId}")
    public BaseResponse deleteArticleByArticleId(@PathVariable("articleId") Integer articleId) {
        ArticleMessage newArticleById = articleOperationService.getNewArticleById(articleId);
        int count = articleMessageMapper.delete(newArticleById);
        if (count <= 0) {
            return ResponseData.out(CodeEnum.NOT_FOUND, "未找到该评论，删除失败");
        }
        return ResponseData.success();
    }

    //后台删除用户的评论
    @DeleteMapping("/deleteComments/{commentId}")
    public BaseResponse deleteCommentsByCommentId(@PathVariable("commentId") Integer commentId) {
        int count = commentMessageMapper.deleteByPrimaryKey(commentId);
        if (count <= 0) {
            return ResponseData.out(CodeEnum.NOT_FOUND, "数据错误");
        }
        return ResponseData.success();
    }

    //后台添加文章
    @PostMapping("/addArticle")
    public BaseResponse addArticle(@RequestBody ArticleMessage articleMessage) {
        articleOperationService.add(articleMessage);
        return ResponseData.success();
    }

    //后台修改文章
    @PutMapping("/editArticle")
    public BaseResponse editArticle(@RequestBody ArticleMessage articleMessage) {
        articleOperationService.update(articleMessage);
        return ResponseData.success();
    }

    //后台根据articleId得到文章详情
    @GetMapping("/getArticleInfo/{articleId}")
    public BaseResponse getArticleInfo(@PathVariable("articleId") Integer articleId) {
        ArticleMessage newArticleById = articleOperationService.getNewArticleById(articleId);
        if (Objects.isNull(newArticleById)) {
            return ResponseData.out(CodeEnum.NOT_FOUND, "数据错误");
        }
        return ResponseData.success(newArticleById);
    }

    //后台根据commentId得到评论的详情
    @GetMapping("/getCommentInfo/{commentId}")
    public BaseResponse getCommentInfo(@PathVariable("commentId") Integer commentId) {
        CommentMessage commentMessage = commentService.getCommentMessage(commentId);
        if (Objects.isNull(commentMessage)) {
            return ResponseData.out(CodeEnum.NOT_FOUND, "数据错误");
        }
        return ResponseData.success(commentMessage);
    }
    //获得被举报的文章列表
    @GetMapping("/getReportArticle")
    public BaseResponse getReportArticle(@RequestParam Integer pageNo,
                                         @RequestParam Integer pageSize){
        PageInfo<NoticeMessage> noticeMessagePageInfo = noticeOperationService.getReportArticleByTypeId(pageNo,pageSize,6);
        if (Objects.isNull(noticeMessagePageInfo)){
            return ResponseData.error(CodeEnum.NOT_FOUND);
        }
        return ResponseData.success(noticeMessagePageInfo);
    }
}
